﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LabMonitorRemote
{

    public partial class LoggerDownloadUserControl : JunkControl
    {
        LoggerDownload download;
        Boolean loggerDownloadActive;

        public LoggerDownloadUserControl(LoggerDownload dl)
        {
            InitializeComponent();
            download = dl;
            lblLoggerName.Text = download.LoggerName;
            lblDownloadProgress.Text = "Initializing ...";

            //Subscribe to LoggerDownload events.
            SubscribeToDownload();
        }

        private void SubscribeToDownload()
        {
            download.BytesChanged += new LoggerDownload.BytesChangedEventHandler(UpdateDownloadIndicators);
            download.TotalBytesChanged += new LoggerDownload.TotalBytesChangedEventHandler(UpdateTotalBytes);
            download.DownloadComplete += new LoggerDownload.DownloadCompleteEventHandler(UpdateDownloadComplete);
            //download.NetworkCommHaulted += new LoggerDownload.NetworkCommHaultedEventHandler(FireTest);
            download.NetworkCommActivityChanged += new LoggerDownload.NetworkCommActivityChangedEventHandler(UpdateNetworkComm);
            download.ConnectionFailed += new LoggerDownload.ConnectionFailedEventHandler(ConnectionFailed);
            download.DownloadCanceled += new LoggerDownload.DownloadCanceledEventHandler(CancelDownloadOnEvent);
            download.DatasetRequested += new LoggerDownload.DatasetRequestedEventHandler(RespondToDatasetRequested);
            // NetworkCommActivityChanged
        }

        private void UnSubscribeFromDownload()
        {
            download.BytesChanged -= new LoggerDownload.BytesChangedEventHandler(UpdateDownloadIndicators);
            download.TotalBytesChanged -= new LoggerDownload.TotalBytesChangedEventHandler(UpdateTotalBytes);
            download.DownloadComplete -= new LoggerDownload.DownloadCompleteEventHandler(UpdateDownloadComplete);
            download.ConnectionFailed -= new LoggerDownload.ConnectionFailedEventHandler(ConnectionFailed);
            download.DownloadCanceled -= new LoggerDownload.DownloadCanceledEventHandler(CancelDownloadOnEvent);
            download.DatasetRequested -= new LoggerDownload.DatasetRequestedEventHandler(RespondToDatasetRequested);
        }



        public void btnCancel_Click(object sender, EventArgs e)
        {
            CancelDownload();
        }

        private void CancelDownloadOnEvent(object sender, EventArgs e)
        {
            CancelDownload();
        }

        private void CancelDownload()
        {
            //Un-Subscribe from LoggerDownload events
            UnSubscribeFromDownload();

            lblDownloadProgress.Text = "Download Canceled";
            lblLoggerName.Text = lblLoggerName.Text + " - CANCELED";
            lblLoggerName.ForeColor = Color.Red;
            btnCancel.Enabled = false;

            // Finally cancel the download
            download.CancelDownload();
        }

        private void RespondToDatasetRequested(object sender, EventArgs e)
        {
            this.Invoke((Action)delegate
            {
                this.lblDownloadProgress.Text = "Building Dataset ...";
            });
        }

        private void UpdateNetworkComm(object sender, EventArgs e)
        {
            this.LoggerDownloadActive = ((LoggerDownload)sender).NetworkCommActive;
            
            Console.WriteLine("{0} Network Communication Haulted!!!!", this.lblLoggerName.Text);
            /*
            this.Invoke((Action)delegate
            {
                this.LoggerDownloadActive = false;
            });
             */
        }

        private void ConnectionFailed(object sender, EventArgs e)
        {
            lblLoggerName.Invoke((Action)delegate
            {
                lblLoggerName.Text = lblLoggerName.Text + " - CONNECTION FAILED!";
                lblLoggerName.ForeColor = Color.Red;
            });
            lblDownloadProgress.Invoke((Action)delegate
            {
                lblDownloadProgress.Text = string.Format("Error connecting to {0}", ((LoggerDownload)sender).LoggerIPAddress);
            });
            btnCancel.Invoke((Action)delegate
            {
                btnCancel.Enabled = false;
            });
        }

        private void UpdateDownloadComplete(object sender, EventArgs e)
        {
            lblLoggerName.Invoke((Action)delegate
            {
                UnSubscribeFromDownload();
                lblLoggerName.Text = lblLoggerName.Text + " - DOWNLOAD COMPLETE";
                lblLoggerName.ForeColor = Color.Green;
                lblDownloadProgress.Text = string.Format("{0} bytes downloaded.", download.TotalBytes);
                btnCancel.Enabled = false;
            });
        }

        private void UpdateTotalBytes(object sender, EventArgs e)
        {
            Int32 totalBytes = ((LoggerDownload)sender).TotalBytes;
            pgrDownloadProgress.Invoke((Action)delegate
            {
                pgrDownloadProgress.Maximum = totalBytes;
            });

        }

        private void UpdateDownloadIndicators(object sender, EventArgs e)
        {
            Int32 bytesDL = ((LoggerDownload)sender).NumBytesDownloaded;
            Int32 totalBytes = ((LoggerDownload)sender).TotalBytes;
            lblDownloadProgress.Invoke((Action)delegate
            {
                lblDownloadProgress.Text = string.Format("{0} of {1} bytes downloaded ...", bytesDL, totalBytes);
            });
            pgrDownloadProgress.Invoke((Action)delegate
            {
                pgrDownloadProgress.Value = bytesDL;
            });
        }
   
        public Boolean LoggerDownloadActive
        {
            get { return loggerDownloadActive; }
            set
            {
                if (loggerDownloadActive != value)
                {
                    loggerDownloadActive = value;
                }
            }
        }

        public override Boolean LoggerCommunicationActive()
        {
            return this.LoggerDownloadActive;
        }


        /*
        private void PrintEvent(object sender, PropertyChangedEventArgs e)
        {
            Int32 bytesDLd = ((LoggerDownload)sender).NumBytesDownloaded;
            textBox1.Invoke((Action)delegate
            {
                //textBox1.Text = e.ToString();
                textBox1.Text = bytesDLd.ToString();
            });
        }
        */
    }
}
